package com.estate.system.service.impl;

import com.estate.common.core.text.Convert;
import com.estate.common.utils.DateUtils;
import com.estate.system.domain.Carousel;
import com.estate.system.domain.CarouselDetail;
import com.estate.system.mapper.CarouselDetailMapper;
import com.estate.system.mapper.CarouselMapper;
import com.estate.system.service.ICarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author estate
 * @date 2022-04-08
 */
@Service
public class CarouselServiceImpl implements ICarouselService
{
    @Autowired
    private CarouselMapper carouselMapper;
    @Resource
    private CarouselDetailMapper carouselDetailMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Carousel selectCarouselById(Long id)
    {
        return carouselMapper.selectCarouselById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param carousel 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Carousel> selectCarouselList(Carousel carousel)
    {
        return carouselMapper.selectCarouselList(carousel);
    }

    /**
     * 新增carousel表一条记录
     * 新增carouseldetail表一条同id的记录
     * 先插入到carousel表,再查询carousel表id
     * @param carousel 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCarousel(Carousel carousel)
    {
        //如果数据超过4条,报错,必须不超过4条(轮播图太多用户体验不好)
        int rowsNum = carouselMapper.selectCarouselNumber();
        System.out.println("rowsNum"+rowsNum);
        if (rowsNum>=4){
            return 0;

        }


        carousel.setCreateTime(DateUtils.getNowDate());
        carouselMapper.insertCarousel(carousel);
        List<Carousel> carousels = carouselMapper.selectCarouselList(carousel);
        Carousel carousel1 = carousels.get(carousels.toArray().length-1);
        System.out.println("carousel1"+carousel1);

        CarouselDetail carouselDetail=new CarouselDetail();
        carouselDetail.setId(carousel1.getId());
        carouselDetail.setDetailPic("[]");
        carouselDetail.setVideoAddress("[]");
        System.out.println("carousel1.getId()"+carousel1.getId());


        return carouselDetailMapper.insertCarouselDetail(carouselDetail);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param carousel 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCarousel(Carousel carousel)
    {


        return carouselMapper.updateCarousel(carousel);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCarouselByIds(String ids)
    {
        return carouselMapper.deleteCarouselByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCarouselById(Long id)
    {
        return carouselMapper.deleteCarouselById(id);
    }
}
